package com.wcity.manage.common.utils;

import org.apache.http.HttpEntity;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.impl.conn.PoolingHttpClientConnectionManager;
import org.apache.http.util.EntityUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.Map;

/**
 * HttpClient工具类
 *
 * @author dy
 */
public class HttpClientUtil {

	/**
	 * POST请求
	 *
	 * @param url
	 * @param data
	 * @param encode
	 * @return
	 * @throws java.io.IOException
	 */
	public static String post( String url, String data, String encode ) throws IOException {

		String ret = "";
		HttpPost post = new HttpPost( url );
		try {
			post.setEntity( new StringEntity( data, encode ) );
			CloseableHttpResponse res = hc.execute( post );
			HttpEntity he = res.getEntity();
			try {
				ret = EntityUtils.toString( he, encode );
				EntityUtils.consume( he );
			} catch ( IOException e ) {
				throw e;
			} finally {
				res.close();
			}
		} catch ( IOException e ) {
			throw e;
		} finally {
			post.abort();
		}
		return ret;
	}
		
	/**
	 * 将参数Map转换为请求url
	 * 
	 * @param params
	 * @return
	 */
	public static String mapParamsToUrl( Map<String, String> params ) {
		
		StringBuilder sb = new StringBuilder();
		for ( String s : params.keySet() ) {
			sb.append( s ).append( "=" ).append( params.get(s) ).append( "&" );
		}
		if ( params.size() > 1 )
			sb.delete( sb.length() - 1, sb.length() );
		return sb.toString();
	}

	/**
	 * 将参数Map转换为请求url, 并作URLencode
	 *
	 * @param params
	 * @return
	 */
	public static String mapParamsToUrlEncode( Map<String, String> params, String encode ) {

		StringBuilder sb = new StringBuilder();

		for ( String s : params.keySet() ) {
			try {
				sb.append( s ).append("=").append( params.get(s) == null ? "" : URLEncoder.encode( params.get(s), encode ) ).append( "&" );
			} catch ( UnsupportedEncodingException e ) {
				continue;
			}
		}
		if ( params.size() > 1 )
			sb.delete( sb.length() - 1, sb.length() );

		return sb.toString();
	}

	private static CloseableHttpClient hc = null;

	private static int TIMEOUT = 60 * 1000;

	private static final Logger log = LoggerFactory.getLogger( HttpClientUtil.class.getName() );

	static {
		PoolingHttpClientConnectionManager cm = new PoolingHttpClientConnectionManager();
		cm.setDefaultMaxPerRoute( 10 );
		cm.setMaxTotal( 200 );
		RequestConfig defaultRequestConfig = RequestConfig.custom().setConnectionRequestTimeout( TIMEOUT )
				.setConnectTimeout( TIMEOUT ).setSocketTimeout( TIMEOUT ).build();
		hc = HttpClients.custom().setConnectionManager( cm ).setDefaultRequestConfig( defaultRequestConfig ).build();
	}
}
